package com.alatus.service.impl;

import com.alatus.config.listener.UploadDataListener;
import com.alatus.constant.Constants;
import com.alatus.mapper.TClueMapper;
import com.alatus.model.TClue;
import com.alatus.query.BaseQuery;
import com.alatus.query.ClueQuery;
import com.alatus.service.ClueService;
import com.alatus.util.JWTUtils;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

@Service
public class ClueServiceImpl implements ClueService {
    @Resource
    private TClueMapper tClueMapper;
    @Override
    public PageInfo<TClue> getClueByPage(Integer current) {
        // 1.设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        // 2.查询
        List<TClue> list = tClueMapper.selectClueByPage(BaseQuery.builder().build());
        // 3.封装分页数据到PageInfo
        PageInfo<TClue> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public void importExcel(InputStream inputStream,String token) {
//        第一个参数是文件,第二个参数是excel模板类,第三个是监听器
        EasyExcel.read(inputStream,TClue.class,new UploadDataListener(tClueMapper,token)).sheet().doRead();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteClue(Integer id) {
        return tClueMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delClueByIds(List<String> idsList) {
        return tClueMapper.deleteByIds(idsList);
    }

    @Override
    public Boolean checkPhone(String phone) {
        return !(tClueMapper.selectByCount(phone) > 0);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveClue(ClueQuery clueQuery) {
        TClue tClue = new TClue();
//        这个工具类的复制要求是两个对象的属性名要相同,属性要相同
        BeanUtils.copyProperties(clueQuery,tClue);
        tClue.setCreateTime(new Date());
//        通过token解析出的用户获取ID作为创建者的ID
        Integer loginId = JWTUtils.parseUserFromJWT(clueQuery.getToken()).getId();
        tClue.setCreateBy(loginId);
        return tClueMapper.insertSelective(tClue);
    }

    @Override
    public TClue getClueById(Integer id) {
        return tClueMapper.selectDetailByPrimaryKey(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int editClue(ClueQuery clueQuery) {
        TClue tClue = new TClue();
//        这个工具类的复制要求是两个对象的属性名要相同,属性要相同
        BeanUtils.copyProperties(clueQuery,tClue);
        tClue.setEditTime(new Date());
//        通过token解析出的用户获取ID作为创建者的ID
        Integer loginId = JWTUtils.parseUserFromJWT(clueQuery.getToken()).getId();
        tClue.setEditBy(loginId);
        return tClueMapper.updateByPrimaryKey(tClue);
    }
}
